package com.augurit.gzcgwsp.commonex.system.service.org.impl;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.augurit.common.dao.OmOrgDao;
import com.augurit.common.entity.OmOrg;
import com.augurit.common.system.convert.OmOrgConverter;
import com.augurit.common.system.service.org.IAcRoleService;
import com.augurit.common.system.service.org.IOmOrgService;
import com.augurit.common.system.web.org.form.OmOrgForm;
import com.augurit.common.system.web.org.form.OmUserForm;
import com.augurit.gzcgwsp.commonex.system.service.org.IOmOrgServiceEX;

@Service
@Transactional
public class OmOrgServiceImplEX implements IOmOrgServiceEX {
	//城管委添加，开始
	@Autowired
	private IOmOrgService omOrgService;
	@Autowired
	private IAcRoleService acRoleService;
	@Autowired
	private OmOrgDao omOrgDao;
	
	public IOmOrgService getOmOrgService() {
		return omOrgService;
	}


    /**
     * 获取某一个用户的最高级父机构
     * @param userId 用户id
     * @return 所有父机构id列表
     */
    @Transactional(readOnly = true)
    public OmOrgForm getTopParentOrgOfUser(Long userId){
        if(userId != null){
            Set<Long> orgIdSet = new HashSet();
            // 获取用户从他所属机构(包括直属机构和间接机构)获得的角色
            List<Long> directParentOrgIds = omOrgService.getAllParentOrgOfUser(userId);
            if(directParentOrgIds != null && directParentOrgIds.size()>0){
            	for (Long orgid: directParentOrgIds) {
                    OmOrgForm orgForm = omOrgService.get(orgid);
                    if (orgForm.getOrgLevel() == 1) return orgForm;
                }
            }            
        }
        return null;
    }
    
  //根据备注的remark里记录的regionIdStr获取该机构下指定的经办人员
    public String getUserByDistrictCodeAndRole(String districtCode, Long roleId ){
    	if(districtCode != null && districtCode.trim().length()>0){
    		Long orgId = getOrgIdByDistrictCode(districtCode);
	    	if(orgId != null){
	    		List<OmUserForm> orgUserList = this.omOrgService.getUsers(orgId);
	    		
	    		//自动经办人指定关联的用户
	    		List<OmUserForm> roleUserList =  acRoleService.getUsersByDirectRole(roleId);
	    		
	    		//用角色下的用户匹配到机构下的用户
	    		for(OmUserForm orgUser:orgUserList){
	    			for(OmUserForm roleUser:roleUserList){
	    				if(roleUser.getUserId() == orgUser.getUserId())
	    					return orgUser.getLoginName();
	    			}
	    		}
	    	}
    	}else{//已知 某个范围里面只有一个人的情况
    		//自动经办人指定关联的用户
    		List<OmUserForm> roleUserList =  acRoleService.getUsersByDirectRole(roleId);
    		if(roleUserList != null && roleUserList.size()>0){
    			OmUserForm omUserForm = roleUserList.get(0);
    			return omUserForm.getLoginName();
    		}
    	}
    	return null;
    }
    
  //根据备注的remark里记录的regionIdStr获取
    private Long getOrgIdByDistrictCode(String districtCode){
    	Long orgId = null;
    	if(districtCode != null && districtCode.trim().length()>0){
    		String hql =  " select ps.orgId from OmOrg ps where ps.remark = '" + districtCode +"'";
    		orgId = this.omOrgDao.findUnique(hql);
    	}
    	return orgId;
    }
    //城管委增加，开结束


	@Override
	public OmOrgForm getUserOrg(Long userId) {
		String hql="select t  from OmOrg t,OmUserOrg uorg where t.orgId=uorg.orgId and uorg.userId="+userId;
		List list=this.omOrgDao.find(hql);
		if(list.size()>0){
			OmOrg org=(OmOrg) list.get(0);
			OmOrgForm orgForm=OmOrgConverter.convertToForm(org);
			return orgForm;
		}
		
		return null;
	}


	@Override
	public OmOrgForm getOrg(Long orgId) {
		OmOrgForm orgForm=this.omOrgService.get(orgId);
		return orgForm;
	}


	@Override
	public OmOrgForm getOrgByOrgName(String orgname) {
		String hql="from OmOrg where orgName='"+orgname+"'";
		List list=this.omOrgDao.find(hql);
		if(list.size()>0){
			OmOrg org=(OmOrg) list.get(0);
			OmOrgForm orgForm=OmOrgConverter.convertToForm(org);
			return orgForm;
		}
		return null;
	}
	
}
